package util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author 李东喜
 * 2009-9-3
 */
public class TreeUtils2 {

    @SuppressWarnings("unchecked")
    public static <K> Map<K, List<Map<String, Object>>> group(Map<String, Object>[] maps, String pname, Class<K> clazz) {
        Map<K, List<Map<String, Object>>> group = new HashMap<K, List<Map<String, Object>>>(0);
        for (Map<String, Object> m : maps) {
            K pid = (K) m.get(pname);
            List<Map<String, Object>> list = group.get(pid);
            if (null != list) {
                list.add(m);
            } else {
                list = new ArrayList<Map<String,Object>>(0);
                list.add(m);
                group.put(pid, list);
            }
        }
        return group;
    }
    
    @SuppressWarnings("unchecked")
    public static <K> void build(List<Map<String, Object>> parents, Map<K, List<Map<String, Object>>> group, String idName, Class<K> clazz) {
        if (null != parents) {
            for (Map<String, Object> parent : parents) {
                Object id = parent.get(idName);
                if (null != id) {
                    List<Map<String, Object>> list = group.get((K) id);
                    if (null != list) {
                        build(list, group, idName, clazz);
                        parent.put("list", list);
                    }
                }
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    public static String output(List<Map<String, Object>> list, int level, String children, IHandler handler) {
        StringBuilder sb = new StringBuilder();
        appendln(sb, sp(level) + "<ul>");
        for (Map<String, Object> m : list) {
            appendln(sb, sp(level) + "<li>" + handler.handle(m));
            Object o = m.get(children);
            if (null != o) {
                appendln(sb, output((List<Map<String, Object>>) o, level + 1, children, handler));
            }
            appendln(sb, sp(level) + "</li>");
        }
        appendln(sb, sp(level) + "</ul>");
        return sb.toString();
    }
    
    @SuppressWarnings("unchecked")
    public static String toString(Map<String, Object> map, int level, String children, IHandler handler) {
        StringBuilder sb = new StringBuilder();
        appendln(sb, "<ul>");
        Object o = map.get(children);
        if (null != o) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) o;
            for (Map<String, Object> m : list) {
                toString(m, level + 1, children, handler);
            }
        }
        appendln(sb, "</ul>");
        return sb.toString();
    }
    
    private static void appendln(StringBuilder sb, String s) {
        sb.append(s).append("\n");
    }
    
    private static String sp(int level) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < level; i++) {
            sb.append("    ");
        }
        return sb.toString();
    }
    
    public interface IHandler {
        String handle(Map<String, Object> map);
    }
}
